#include"Blockqueue.hpp"
#include <sys/types.h>
#include "Task.hpp"
#include <unistd.h>
template<class T,class S>
class BlockqueueList{
public:
    Blockqueue<T>* bq_p;
    Blockqueue<S>* bq_s;
};
void* p_task(void* args){

    BlockqueueList<Task,Save>* bq_L = static_cast<BlockqueueList<Task,Save>*>(args);
    Blockqueue<Task>* bq_p = bq_L->bq_p;
    while (true){
        int x = rand()%10;
        int y = rand()%5 ;
        int op =optionlist[rand() % optionlist.size()];
        
        Task caltsk(x,y,op,mymath);


        std::cout<<caltsk.tostring()<<std::endl;
        bq_p->push(caltsk);
    }
    

    return nullptr;
}

void* c_task(void* args){
    BlockqueueList<Task,Save>* bq_L = static_cast<BlockqueueList<Task,Save>*>(args);
    Blockqueue<Task>* bq_p = bq_L->bq_p;
    Blockqueue<Save>* bq_s = bq_L->bq_s;
    while (true){
        Task get_task;
        bq_p->pop(&get_task);
        std::cout<<"正在保存数据"<<std::endl;
        std::string result = get_task();//我已经处理完任务，将结果包装成类丢到保存队列中
        
        Save s(result,save);
        bq_s->push(s); //将打包的数据发送给s_task线程处理
        
        sleep(1);
    }
    return nullptr;
}

void* s_task(void* args){

    BlockqueueList<Task,Save>* bq_L = static_cast<BlockqueueList<Task,Save>*>(args);
    Blockqueue<Save>* bq_s = bq_L->bq_s;

    while(true){
        Save s;
        bq_s->pop(&s);
        s();
        std::cout<<"保存任务成功"<<std::endl;
    }
    return nullptr;
}




int main(){
    pthread_t t1,t2,t3;
    srand((unsigned long)time(0) ^ getpid());

    BlockqueueList<Task,Save>* bq_L = new BlockqueueList<Task,Save>();
    bq_L->bq_p = new Blockqueue<Task>();
    bq_L->bq_s = new Blockqueue<Save>();



    pthread_create(&t1,nullptr,c_task,bq_L);
    pthread_create(&t2,nullptr,p_task,bq_L);
    pthread_create(&t3,nullptr,s_task,bq_L);


    pthread_join(t1,nullptr);
    pthread_join(t2,nullptr);
    pthread_join(t3,nullptr);


    return 0;
}